package com.ghck.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.controller.access.ArgIndexForPrintLog;
import com.ghck.server.model.DepartmentDict;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.ResultObject;
import com.ghck.server.service.DepartmentDictService;
import com.ghck.server.service.HospitalDictService;
import com.ghck.server.service.RegionDictService;

/**
 * 医院 
 *
 */
@RequestMapping("/hospital")
@RestController
public class HospitalController {
	Logger logger = LoggerFactory.getLogger(HospitalController.class);

	@Autowired
	HospitalDictService hospitalDictService;

	@Autowired
	DepartmentDictService departmentDictService;

	@Autowired
	RegionDictService regionDictService;

	/**
	 *科室列表
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/department/fetch_all", method = RequestMethod.GET)
	public ResultList<DepartmentDict> fetchAllDepartment(){
		List<DepartmentDict> departments = departmentDictService.findAllWithCache();
		return new ResultList<>(departments);
	}

	/**
	 *根据parentId科室列表
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/department/by_parent_id/{parentId}", method = RequestMethod.GET)
	public ResultList<DepartmentDict> fetchByParentId(@PathVariable("parentId") Integer parentId){
		List<DepartmentDict> departments = departmentDictService.findChildrenByParentId(parentId);
		return new ResultList<>(departments);
	}

	/**
	 * 根据ID查询科室
	 * @param id
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/by_id/{id}", method = RequestMethod.GET)
	public ResultObject<DepartmentDict> fetchById(@PathVariable("id") Integer id){
		DepartmentDict department = departmentDictService.findByIdWithCache(id);
		return new ResultObject<>(department);
	}



//	//================================ 管理员接口 ===================================================
//
//	/**
//	 * 添加医院-医院管理
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	public ResultObject<Integer> addHospital(@RequestBody HospitalDict hospitalDict){
//		if(StringUtils.isEmpty(hospitalDict.getName())){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"名字不能为空");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//		if(!IntegerUtil.isPositiveInteger(hospitalDict.getLevelId())){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"医院级别为空或不合法");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//		if(!IntegerUtil.isPositiveInteger(hospitalDict.getRegionId())){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"所属区域为空或不合法");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//		if(StringUtils.isEmpty(hospitalDict.getAddress())){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"地址不能为空");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//		if(hospitalDict.getLongitude()==null || hospitalDict.getLongitude()<0 || hospitalDict.getLongitude()>180){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度为空或不合法");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//		if(hospitalDict.getLatitude()==null || hospitalDict.getLatitude()<0 || hospitalDict.getLatitude()>90){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度为空或不合法");
//			logger.info("添加医院失败",exception);
//			throw exception;
//		}
//
//		Integer id = hospitalDictService.insertHospitalDict(hospitalDict);
//
//		return new ResultObject<>(id);
//
//	}
//
//
//
//	/**
//	 * 修改医院-医院管理
//	 * 不能修改InUsed;
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/modify/{hosptialId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> modifyHospital(@PathVariable("hosptialId") Integer hosptialId,@RequestBody HospitalDict hospitalDict){
//		if(!IntegerUtil.isPositiveInteger(hosptialId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
//			logger.info("修改医院失败",exception);
//			throw exception;
//		}
//		HospitalDict db = hospitalDictService.findByIdInCache(hosptialId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"修改的医院不存在");
//			logger.info("修改医院失败",exception);
//			throw exception;
//		}
//		if(hospitalDict.getLongitude()==null &&( hospitalDict.getLongitude()<0 || hospitalDict.getLongitude()>180)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"经度不合法");
//			logger.info("修改医院失败",exception);
//			throw exception;
//		}
//		if(hospitalDict.getLatitude()!=null &&( hospitalDict.getLatitude()<0 || hospitalDict.getLatitude()>90)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"纬度不合法");
//			logger.info("修改医院失败",exception);
//			throw exception;
//		}
//
//		hospitalDict.setInUsed(null); //不能修改InUsed;
//
//		hospitalDict.setId(hosptialId);
//
//		hospitalDictService.updateHospitalDict(hospitalDict);
//
//		return new ResultObject<>(hosptialId);
//	}
//
//
//
//	/**
//	 * 禁用医院-医院管理
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/disable/{hosptialId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> disableHospital(@PathVariable("hosptialId") Integer hosptialId){
//		if(!IntegerUtil.isPositiveInteger(hosptialId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
//			logger.info("禁用医院失败",exception);
//			throw exception;
//		}
//		HospitalDict db = hospitalDictService.findByIdInCache(hosptialId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"禁用医院不存在");
//			logger.info("禁用医院失败",exception);
//			throw exception;
//		}
//
//		db.setInUsed(0);
//
//		hospitalDictService.updateHospitalDict(db);
//
//		return new ResultObject<>(hosptialId);
//
//	}
//
//
//
//	/**
//	 * 启用医院-医院管理
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/enable/{hosptialId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> enableHospital(@PathVariable("hosptialId") Integer hosptialId){
//		if(!IntegerUtil.isPositiveInteger(hosptialId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的医院ID必须是正整数");
//			logger.info("启用医院失败",exception);
//			throw exception;
//		}
//		HospitalDict db = hospitalDictService.findHospitalDictById(hosptialId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"启用医院不存在");
//			logger.info("启用医院失败",exception);
//			throw exception;
//		}
//
//		db.setInUsed(1);
//
//		hospitalDictService.updateHospitalDict(db);
//
//		return new ResultObject<>(hosptialId);
//
//	}
//
//	/**
//	 * 拉取医院列表
//	 * 
//	 * 直接查询数据库，管理平台速度慢些无所谓
//	 * 
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/list", method = RequestMethod.GET)
//	public ResultList<HospitalDict> questHospital(HospitalDictQuery query){
//		ResultList<HospitalDict> resultList = new ResultList<>();
//		if(IntegerUtil.isPositiveInteger(query.getRegionId())){
//			query.setRegionIds(regionDictService.findSelfAllChildrenId(query.getRegionId()));
//		}
//
//		List<HospitalDict> result = hospitalDictService.findHospitalDictByQuery(query);
//
//		Integer count = hospitalDictService.countHospitalDictByQuery(query);
//
//		query.setCount(count);
//		resultList.setItemList(result);
//
//		resultList.setExt(query);
//
//		return resultList;
//	}
//
//
//	/**
//	 * 添加科室
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/department/add", method = RequestMethod.POST)
//	public ResultObject<Integer> addDepartment(@RequestBody DepartmentDict departmentDict){
//		String name = departmentDict.getName();
//		if(StringUtils.isEmpty(name)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"名字不能为空");
//			logger.info("添加科室失败",exception);
//			throw exception;
//		}
//		if(departmentDict.getParentId()==null){
//			departmentDict.setParentId(0);
//		}
//
//		DepartmentDict dbByName = departmentDictService.findByName(name);
//		if(dbByName!=null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"科室已存在，不需添加");
//			logger.info("添加科室失败",exception);
//			throw exception;
//		}
//
//		Integer id = departmentDictService.insertDepartmentDict(departmentDict);
//
//		return new ResultObject<Integer>(id);
//
//	}
//
//
//	/**
//	 * 修改科室
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/department/modify/{departmentId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> modifyDepartment(@PathVariable("departmentId") Integer departmentId, @RequestBody DepartmentDict departmentDict){
//
//		if(!IntegerUtil.isPositiveInteger(departmentId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
//			logger.info("修改科室失败",exception);
//			throw exception;
//		}
//		DepartmentDict db = departmentDictService.findByIdWithCache(departmentId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"修改的科室不存在");
//			logger.info("修改科室失败",exception);
//			throw exception;
//		}
//		departmentDict.setInUsed(null);
//		Integer idInteger = departmentDictService.updateDepartmentDict(departmentDict);
//		return new ResultObject<Integer>(idInteger);
//	}
//
//	/**
//	 * 禁用科室
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/department/disable/{departmentId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> disableDepartment(@PathVariable("departmentId") Integer departmentId){
//		if(!IntegerUtil.isPositiveInteger(departmentId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
//			logger.info("禁用科室失败",exception);
//			throw exception;
//		}
//		DepartmentDict db = departmentDictService.findByIdWithCache(departmentId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"禁用科室不存在");
//			logger.info("禁用科室失败",exception);
//			throw exception;
//		}
//
//		db.setInUsed(0);
//
//		departmentDictService.updateDepartmentDict(db);
//
//		return new ResultObject<>(departmentId);
//
//	}
//
//	/**
//	 * 启用科室 
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/department/enable/{departmentId}", method = RequestMethod.PUT)
//	public ResultObject<Integer> enableDepartment(@PathVariable("departmentId") Integer departmentId){
//		if(!IntegerUtil.isPositiveInteger(departmentId)){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"指定的科室ID必须是正整数");
//			logger.info("启用科室失败",exception);
//			throw exception;
//		}
//		DepartmentDict db = departmentDictService.findById(departmentId);
//		if(db==null){
//			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"启用科室不存在");
//			logger.info("启用科室失败",exception);
//			throw exception;
//		}
//
//		db.setInUsed(1);
//		departmentDictService.updateDepartmentDict(db);
//
//		return new ResultObject<>(departmentId);
//	}
//
//
//	/**
//	 * 查询科室
//	 */
//	@ArgIndexForPrintLog(indexes={})
//	@RequestMapping(value = "/department/list", method = RequestMethod.GET)
//	public ResultList<DepartmentDict> questHospital(DepartmentDictQuery query){
//		ResultList<DepartmentDict> resultList = new ResultList<>();
//
//		List<DepartmentDict> result = departmentDictService.findDepartmentDictByQuery(query);
//
//		Integer count = departmentDictService.countDepartmentDictByQuery(query);
//
//		query.setCount(count);
//		resultList.setItemList(result);
//
//		resultList.setExt(query);
//
//		return resultList;
//	}
}
